<?php
class koudai_dzControl extends skymvc{
	
	public function __construct(){
		
		parent::__construct();		
	}
	
	public function onDefault(){
		$status=max(1,get('status','i'));
		$where=" adminid=".ADMINID." AND status=".$status;
		$baseurl=$url=APPKOUDAI."?m=koudai_dz&status=".$status;
		
		$start=get('per_page','i');
		$limit=60;
		$index='';
			switch(get('index')){
				case 'asc':
						$index='asc';
					break;
				case 'desc':
						$index='desc';
					break;
			}
			switch(get('orderby')){
				case "id":
					$order=" order_id $index";
					break;
				case "sushe":
					$order=" sushe $index";
					break;
				default:
					$order=" order_id DESC";
					break;
			}
		$option=array(
			"where"=>$where,
			"order"=>$order,
			"start"=>$start,
			"limit"=>$limit
		);
		$rscount=true;
		$data=M("koudai_dz")->select($option,$rscount);
		$scorder=array();
		if($data){
			
			foreach($data as $k=>$v){
				$oids[]=$v['order_id'];
			}
			
			$orders=M("koudai_order")->id_list(array(
				"where"=>" order_id in("._implode($oids).") ",
				"order"=>$order
			));
			 
			foreach($data as $k=>$v){
				$v['order']=$o=$orders[$v['order_id']];
				$v['sc_id']=$o['sc_id'];
				$v['products']=$this->orderproduct($v['order_id']);
				$v['address']=M("koudai_order_address")->selectRow(array("where"=>"order_id=".$o['order_id'],"order"=>"id DESC"));	
				$v['gznum']=M("koudai_order")->getOne("select sum(gznum) as ct from ".table('koudai_order_product')." where order_id=".$o['order_id']." ");
				$v['jsnum']=M("koudai_order")->getOne("select sum(jsnum) as ct from ".table('koudai_order_product')." where order_id=".$o['order_id']." ");
				$v['gzmoney']=M("koudai_order")->getOne("select sum(gznum*price) as ct from ".table('koudai_order_product')." where order_id=".$o['order_id']." ");
				$v['jsmoney']=M("koudai_order")->getOne("select sum(jsnum*price) as ct from ".table('koudai_order_product')." where order_id=".$o['order_id']." ");
				$data[$k]=$v;
			}
			
			foreach($data as $k=>$v){
				
				$scids[]=$v['sc_id'];
				$data[$k]=$v;
			}
			$sitecitys=M("site_city")->id_list(array("where"=>"siteid=".SITEID));
			foreach($data as $v){
				 
				$scorder[$v['sc_id']]['order'][]=$v;
			}
			
			
		}
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$this->smarty->assign(array(
			"data"=>$data,
			"pagelist"=>$pagelist,
			"scorder"=>$scorder,
			"rscount"=>$rscount,
			"sitecitys"=>$sitecitys,
			"order_status_list"=>$order_status_list,
			"order_type_list"=>$order_type_list,
			"order_ispay"=>$order_ispay,
			"baseurl"=>$baseurl
		));
		$this->smarty->display("koudai_dz/index.html");
	}
	
	public function onGoShow(){
		$order_id=get('order_id','i');
		$_GET['id']=$this->onGet();
		
		$this->onSHow();
	}
	
	public function onSHow(){
		$id=get_post('id','i');
		$dzRow=M("koudai_dz")->selectRow("id=".$id);		 
		if($dzRow['adminid']!=ADMINID){
			$this->goAll("该对账已经被人抢去了");
		}
		$order=M("koudai_order")->selectRow("order_id=".$dzRow['order_id']);
		$address=M("koudai_order_address")->selectRow("order_id=".$dzRow['order_id']);
		$order_product=$this->orderproduct($order['order_id']);
		$gznum=$thnum=$jsnum=0;
		$gzmoney=$thmoney=$jsmoney=0;
		foreach($order_product  as $v){
			$gznum +=$v['gznum'];
			$gzmoney+=$v['gznum']*$v['price'];
			$thnum+=$v['thnum'];
			$thmoney+=$v['thnum']*$v['price'];
			$jsnum+=$v['jsnum'];
			$jsmoney+=$v['jsnum']*$v['price'];
		}
		$this->smarty->assign(array(
			"dz"=>$dzRow,
			"order"=>$order,
			"addr"=>$address,
			"order_product"=>$order_product,
			"gznum"=>$gznum,
			"gzmoney"=>$gzmoney,
			"thnum"=>$thnum,
			"thmoney"=>$thmoney,
			"jsnum"=>$jsnum,
			"jsmoney"=>$jsmoney
		));
		$this->smarty->display("koudai_dz/show.html");
	}
	
	public function onorder(){
		$status_w=" AND status in(0,1,2,3,10)  ";
		$scids=M("koudai_admin_area")->selectCols(array(
			"fields"=>"sc_id",
			"where"=>" adminid=".ADMINID
		)); 
		$where=" shopid=".SHOPID."  AND isdz=1 AND isadd=0 ";
		if(!empty($scids)){
			$where.=" AND sc_id in("._implode($scids).") ";
		}else{
			$where.=" AND 1=2 ";
		}
		 
		$baseurl=$url=APPKOUDAI."?m=koudai_dz&a=order";
		$start=get('per_page','i');
		$limit=100;
		$where.=$status_w;
		$index='';
			switch(get('index')){
				case 'asc':
						$index='asc';
					break;
				case 'desc':
						$index='desc';
					break;
			}
			switch(get('orderby')){
				case "id":
					$order=" order_id $index";
					break;
				case "sushe":
					$order=" sushe $index";
					break;
				default:
					$order=" order_id DESC";
					break;
			}
		$option=array(
			"where"=>$where,
			"order"=>$order,
			"start"=>$start,
			"limit"=>$limit
		);
		$rscount=true;
		$data=M("koudai_order")->select($option,$rscount);
		$scorder=array();
		if($data){
			foreach($data as $k=>$v){
				$v['address']=M("koudai_order_address")->selectRow(array("where"=>"order_id=".$v['order_id'],"order"=>"id DESC"));	
				$v['num']=M("koudai_order")->getOne("select sum(amount) as ct from ".table('koudai_order_product')." where order_id=".$v['order_id']." ");
				$scids[]=$v['sc_id'];
				$data[$k]=$v;
			}
			$sitecitys=M("site_city")->id_list(array("where"=>"siteid=".SITEID));
			foreach($data as $v){
				 
				$scorder[$v['sc_id']]['order'][]=$v;
			}
			
		}
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$order_status_list=$this->config_item('order_status_list');
		$order_type_list=$this->config_item('order_type_list');
		$order_ispay=$this->config_item('order_ispay');
		$this->smarty->assign(array(
			"data"=>$data,
			"rscount"=>$rscount,
			"scorder"=>$scorder,
			"pagelist"=>$pagelist,
			"sitecitys"=>$sitecitys,
			"baseurl"=>$baseurl,
			"order_status_list"=>$order_status_list,
			"order_type_list"=>$order_type_list,
			"order_ispay"=>$order_ispay,
			
		));
		$this->smarty->display("koudai_dz/order.html");
	}
	
	
	public function onundz(){
		$status_w=" AND status in(0,1,2,3,10)  ";
		$scids=M("koudai_admin_area")->selectCols(array(
			"fields"=>"sc_id",
			"where"=>" adminid=".ADMINID
		)); 
		$where=" shopid=".SHOPID."  AND isdz=0 AND isadd=0 ";
		if(!empty($scids)){
			$where.=" AND sc_id in("._implode($scids).") ";
		}else{
			$where.=" AND 1=2 ";
		}
		 
		$baseurl=$url=APPKOUDAI."?m=koudai_dz&a=unorder";
		$start=get('per_page','i');
		$limit=100;
		$where.=$status_w;
		$index='';
			switch(get('index')){
				case 'asc':
						$index='asc';
					break;
				case 'desc':
						$index='desc';
					break;
			}
			switch(get('orderby')){
				case "id":
					$order=" order_id $index";
					break;
				case "sushe":
					$order=" sushe $index";
					break;
				default:
					$order=" order_id DESC";
					break;
			}
		$option=array(
			"where"=>$where,
			"order"=>$order,
			"start"=>$start,
			"limit"=>$limit
		);
		$rscount=true;
		$data=M("koudai_order")->select($option,$rscount);
		$scorder=array();
		if($data){
			foreach($data as $k=>$v){
				$v['address']=M("koudai_order_address")->selectRow(array("where"=>"order_id=".$v['order_id'],"order"=>"id DESC"));	
				$v['num']=M("koudai_order")->getOne("select sum(amount) as ct from ".table('koudai_order_product')." where order_id=".$v['order_id']." ");
				$scids[]=$v['sc_id'];
				$data[$k]=$v;
			}
			$sitecitys=M("site_city")->id_list(array("where"=>"siteid=".SITEID));
			foreach($data as $v){
				 
				$scorder[$v['sc_id']]['order'][]=$v;
			}
			
		}
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$order_status_list=$this->config_item('order_status_list');
		$order_type_list=$this->config_item('order_type_list');
		$order_ispay=$this->config_item('order_ispay');
		$this->smarty->assign(array(
			"data"=>$data,
			"rscount"=>$rscount,
			"scorder"=>$scorder,
			"pagelist"=>$pagelist,
			"sitecitys"=>$sitecitys,
			"baseurl"=>$baseurl,
			"order_status_list"=>$order_status_list,
			"order_type_list"=>$order_type_list,
			"order_ispay"=>$order_ispay,
			
		));
		$this->smarty->display("koudai_dz/undz.html");
	}
	
	public function onDuiZhang(){
		
		$order_id=get('order_id','i');
		$data=M("koudai_order")->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		 
		if($data['status']!=3){
			$this->goall("该订单还未配送完成，不能对账哦",1);
		}
		
		$address=M("koudai_order_address")->selectRow("order_id=".$order_id);
		$data['products']=$this->orderproduct($order_id);
		$data['total_num']=M("koudai_order_product")->selectOne(array(
			"where"=>"order_id=".$order_id,
			"fields"=>" sum(amount) "
		));
		$gz_money=$th_money=$js_money=0;
		$gz_num=$th_num=$js_num=0;
		if($data['products']){
			foreach($data['products'] as $v){
				$gz_money +=$v['gznum']*$v['order_price'];
				$th_money +=$v['thnum']*$v['order_price'];
				$js_money +=$v['jsnum']*$v['order_price'];
				$gz_num+=$v['gznum'];
				$th_num+=$v['thnum'];
				$js_num+=$v['jsnum'];
			}
		}
		$this->smarty->assign(array(
			"data"=>$data,
			"gz_money"=>$gz_money,
			"th_money"=>$th_money,
			"js_money"=>$js_money,
			"gz_num"=>$gz_num,
			"th_num"=>$th_num,
			"js_num"=>$js_num,
			"addr"=>$address
		));
		switch(get('op')){
			case 'tuihuo':
				$tpl="duizhang_tuihuo.html";
				break;
			case 'jiesuan':
				$tpl="duizhang_jiesuan.html";
				break;
			default:
				$tpl="duizhang.html";
				break;
		}
		if($data['isdz']){
			$this->goAll("已对账");
		}else{
			$this->smarty->display("koudai_dz/".$tpl);
		}
	}
	
	
	public function onDuiZhangNum(){
		$order_id=get('order_id','i');
		$data=M("koudai_order")->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
	 
		if(!$data['fenqi']){
			$this>goAll("该订单不是分期订单，无法对账");
		}
		
		if($data['status']!=3){
			$this->goall("该订单还未完成，不能对账哦",1);
		}
		
		if($data['isdz']){
			$this->goall("该订单已经对账了",1);
		}
		
		if($data['isadd']){
			$this->goall("该订单已经自动挂账新订单了",1);
		}
		
		$id=get('id','i');
		$product=M("koudai_order_product")->selectRow("id=".$id);
		if(isset($_GET['thnum'])){
			$p=M("product")->selectRow("id=".$product['productid']);
			if(!$p['canback']){
				$this->goall("有产品不能退货，数据出错",1);
			}
		}
		$pdata['gznum']=isset($_GET['gznum'])?get('gznum','i'):$product['gznum'];
		$pdata['thnum']=isset($_GET['thnum'])?get('thnum','i'):$product['thnum'];
		if(($pdata['gznum']+$pdata['thnum'])>$product['amount']){
			$this->goAll("保存失败，对账数量超出",1);
		}
		$pdata['jsnum']=$product['amount']-$pdata['gznum']-$pdata['thnum'];
		if(($pdata['gznum']+$pdata['thnum']+$pdata['jsnum'])>$product['amount']){
			$this->goAll("保存失败，对账数量超出",1);
		}
		M("koudai_order_product")->update($pdata,"id=".$id);
		$products=$this->orderproduct($order_id);
		$gz_money=$th_money=$js_money=0;
		$gz_num=$th_num=$js_num=0;
		if($products){
			foreach($products as $v){
				$gz_money +=$v['gznum']*$v['order_price'];
				$th_money +=$v['thnum']*$v['order_price'];
				$js_money +=$v['jsnum']*$v['order_price'];
				$gz_num+=$v['gznum'];
				$th_num+=$v['thnum'];
				$js_num+=$v['jsnum'];
			}
		}
		$data=array(
			"gz_money"=>$gz_money,
			"th_money"=>$th_money,
			"js_money"=>$js_money,
			"gz_num"=>$gz_num,
			"th_num"=>$th_num,
			"js_num"=>$js_num,
		);
		$this->goAll("保存成功",0,$data);
	}
	
	 
	
	public function onDuiZhangSave(){
		$order_id=get('order_id','i');
		$data=M("koudai_order")->selectRow(array("where"=>"order_id=".$order_id));
		
		if(empty($data)) $this->goall("参数出错",1);
		if($data['isdz']){
			 $this->goall("已对账了",1);
		}
		if(!$data['fenqi']){
			$this>goAll("该订单不是分期订单，无法对账");
		}
		if($data['status']!=3){
			$this->goall("该订单还未完成，不能对账哦",1);
		}
		$pay_type=get_post('paytype','h');
		 
		$kdproducts=M("koudai_order_product")->select(array(
			"where"=>"order_id=".$order_id
		));
		 
		$dzmoney=0;
		foreach($kdproducts as $v){
			$dzmoney+=$v['price']*$v['jsnum'];
		}
		$dzpay=2;
		M("koudai_order")->update(array(
			"isdz"=>1,
			"dzmoney"=>$dzmoney,
			"dzpay"=>$dzpay
		),"order_id=".$order_id);
		//确认
		if($data['fenqi']==2){
			M("koudai_order_ssuser")->update(array(
				"status"=>1
			),"order_id=".$order_id);
		}
		/*对账记录*/
		M("koudai_dz_log")->insert(array(
			"order_id"=>$order_id,
			"userid"=>M("login")->userid,
			"sushe"=>$data['sushe'],
			"dateline"=>time(),
			"k"=>"dz_add"
		));
		/*End对账记录*/
		$id=$this->onGet();
		if($id){
			$this->goALl("对账完成",0,0,APPKOUDAI."?m=koudai_dz&a=show&id=".$id);
		}else{
			$this->goALl("数据出错了",1,0,APPKOUDAI."?m=koudai_dz&a=undz");
		}
		
	}
	
	public function onConfirm(){
		
		$id=get_post('id','i');
		
		$dz=M("koudai_dz")->selectRow("id=".$id);
		
		if(empty($dz)){
			$this->goAll("数据出错"); 
		}
		 
		if($dz['status']==3) $this->goAll("该订单已经对账过了"); 
		$order_id=$dz['order_id'];
		$order=M("koudai_order")->selectRow("order_id=".$dz['order_id']);
		if(empty($order)) $this->goAll("订单出错"); 
		M("koudai_dz")->begin();
		//增加用户信用额
		$youka=M("youka")->get($dz['userid'],$order['fenqi']);
		M("youka")->addMoney(
			array(
				"youka"=>$order['youka'],
				"userid"=>$dz['userid'],
				"typeid"=>$order['fenqi'],
				"money"=>$dz['money'],
				"content"=>"您获得了[money]元",
				"siteid"=>SITEID
			) 
		);
		
		//分配收入
		$dzmoney=$dz['money']*KD_DZ_MONEY;
		$qdmoney=$dz['money']*KD_QD_MONEY;
		//1对账员
		
		M("koudai_admin")->addMoney(array(
			"income"=>$dzmoney,
			"balance"=>$dzmoney,
			"shopid"=>$order['shopid'],
			"adminid"=>ADMINID,
			"content"=>"您的订单完成了，获得[money]元"
		));
		/* 
		//签单员
		M("koudai_admin")->addMoney(array(
			"income"=>$qdmoney,
			"balance"=>$qdmoney,
			"shopid"=>$order['shopid'],
			"adminid"=>$order['qdadmin'],
			"content"=>"您的订单完成了，获得[money]元"
		)); 
	 	*/
	 	/**----处理退货--------**/
		 
		$k=get_post('k');
		 
		
		$data=array(
			"siteid"=>$order['siteid'],
			"order_id"=>$order['order_id'],
			"dateline"=>time(),
			
		);
		if($k==1){
			$data['shopid']=$order['shopid'];
			$data['k']="koudai";
		}else{
			//选出夜猫店
			$shop=M("ymdian")->selectRow("sc_id=".$order['sc_id']);
			if(empty($shop)){
				$this->goALl("本栋楼还没有夜猫店，只能退货至口袋店",1);
			}
			$data['shopid']=$shop['id'];
			$data['k']="ymdian";
		}
		
		M("koudai_order_thshop")->insert($data);
		M("koudai_dz")->update(array(
			"status"=>3,
			"thshop"=>$k
		),"id=".$id);
		M("koudai_order")->commit();
		$this->goAll("对账完成");
	}
	
	
	
	public function orderproduct($order_id){
		
		$order_product=M("koudai_order_product")->select(array("where"=>"order_id=".intval($order_id)));
		if($order_product){
			foreach($order_product as $k=>$v){
				$productids[]=$v['productid'];
			}
			$products=M("product")->getListByIds($productids);
			foreach($order_product as $k=>$v){
				$p=$products[$v['productid']];
				$p['order_price']=$v['price'];
				$p['amount']=$v['amount'];
				$p['total_money']=$v['amount']*$v['price'];
				$p['gznum']=$v['gznum'];
				$p['thnum']=$v['thnum'];
				$p['jsnum']=$v['jsnum'];
				$p['order_product_id']=$v['id'];
				$order_product[$k]=$p;
			}
		}
		return $order_product;	
	}
	
	public function onGet(){
		$order_id=get('order_id','i');
		$order=M("koudai_order")->selectRow("order_id=".$order_id);
		if($order['isdz']!=1 or $order['isadd']!=0){
			$row=M("koudai_dz")->selectRow("order_id=".$order_id);
			 
			return $row['id'];
		}
		$ps=M("koudai_order_product")->select(array(
			"where"=>"order_id=".$order_id
		));
		$jsmoney=0;
		if($ps){
			foreach($ps as $p){					
				$jsmoney+=$p['jsnum']*$p['price'];	
			}
				
		}
		$data=array(
			"userid"=>$order['userid'],
			"shopid"=>$order['shopid'],
			"siteid"=>$order['siteid'],
			"adminid"=>ADMINID,
			"order_id"=>$order_id,
			"typeid"=>1,
			"dateline"=>time(),
			"susheid"=>$order['susheid'],
			"money"=>$jsmoney,
			"sushe"=>$order['sushe']
		);
		$id=M("koudai_dz")->insert($data);
		M("koudai_order")->update(array(
			"isdz"=>2,
			"dzadmin"=>ADMINID
		),"order_id=".$order_id);
		return $id;
	}
	
	
	public function onThShop(){
		
		$order_id=get_post('order_id','i');
		$k=get_post('k');
		$id=get_post('id','i');
		$order=M("koudai_order")->selectRow("order_id=".$order_id);
		if(empty($order)) $this->goAll("订单出错");
		$dz=M("koudai_dz")->selectRow("id=".$id);
		if($dz['status']!=2){
			$this->goAll("数据出错");
		}
		M("koudai_order")->begin();
		$data=array(
			"siteid"=>$order['siteid'],
			"order_id"=>$order['order_id'],
			"dateline"=>time(),
			
		);
		if($k==1){
			$data['shopid']=$order['shopid'];
			$data['k']="koudai";
		}else{
			//选出夜猫店
			$shop=M("ymdian")->selectRow("sc_id=".$order['sc_id']);
			if(empty($shop)){
				M("koudai_order")->rollback();
				$this->goALl("本栋楼还没有夜猫店，只能退货至口袋店",1);
			}
			$data['shopid']=$shop['id'];
			$data['k']="ymdian";
		}
		
		M("koudai_order_thshop")->insert($data);
		M("koudai_dz")->update(array(
			"status"=>3
		),"id=".$id);
		M("koudai_order")->commit();
		$this->goAll("退货成功");
	}
}
?>